<?php

// $Id$


class TCMS_Image extends Tangra_Class {
	const ORIGINAL_IMAGE_SUFFIX = '_orig';
	const FIXED_SIZE_IMAGE_SUFFIX = '_fix';
	const THUMBNAIL1_IMAGE_SUFFIX = '_t1';
	const THUMBNAIL2_IMAGE_SUFFIX = '_t2';

	private $id;

	private $orig_filename;
	private $size = 0;
	private $size_x = 0;
	private $size_y = 0;
	private $path = '';
	private $filename = '';
	private $note = '';
	private $alt = '';


	public function set_id($id) {
		tangra_if_not_int_throw_e($id);

		$this->id = $id;
	}


	public function get_id() {
		return $this->id;
	}


	public function set_orig_filename($orig_filename) {
		$this->orig_filename = $orig_filename;
	}


	public function get_orig_filename() {
		return $this->orig_filename;
	}


	public function set_size($size) {
		tangra_if_not_int_throw_e($size);

		$this->size = $size;
	}


	public function get_size() {
		return $this->size;
	}


	public function set_size_x($size_x) {
		tangra_if_not_int_throw_e($size_x);

		$this->size_x = $size_x;
	}


	public function get_size_x() {
		return $this->size_x;
	}



	public function set_size_y($size_y) {
		tangra_if_not_int_throw_e($size_y);

		$this->size_y = $size_y;
	}


	public function get_size_y() {
		return $this->size_y;
	}


	public function set_path($path) {
		$this->path = $path;
	}


	public function get_path() {
		return $this->path;
	}


	public function set_filename($filename) {
		$this->filename = $filename;
	}


	public function get_filename() {
		return $this->filename;
	}


	public function set_note($note) {
		$this->note = $note;
	}


	public function get_note() {
		return $this->note;
	}


	public function set_alt($alt) {
		$this->alt = $alt;
	}


	public function get_alt() {
		return $this->alt;
	}


	public static function is_valid_extension($file_name, $allowed_extensions_comma_list) {
		$ret = false;

		$last_dot = strrpos($file_name, '.');
		if ($last_dot !== false) {
			$file_extension = trim(substr($file_name, $last_dot + 1));
			$extensions_list = explode(',', $allowed_extensions_comma_list);
			foreach($extensions_list as $ext) {
				if (strtolower(trim($ext)) == strtolower($file_extension)) {
					$ret = true;
					break;
				}
			}
		}

		return $ret;
	}


	public static function split_bg_colors($bgcolor_hex) {
		$ret = array();

		if (strlen($bgcolor_hex) == 6) {
			$red = substr($bgcolor_hex, 0, 2);
			$green = substr($bgcolor_hex, 2, 2);
			$blue = substr($bgcolor_hex, 4);

			$ret['red'] = hexdec($red);
			$ret['green'] = hexdec($green);
			$ret['blue'] = hexdec($blue);
		} else {
			throw new Tangra_Module_Exception('Invalid bgcolor: '.$bgcolor_hex);
		}

		return $ret;
	}
}